package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * 6.10、知识信息---最后一次登录到当前时间新增的知识
 * @author Tallen Zhai
 *
 */
public class KnowledgeListEntity extends BaseEntity {

	private static final long serialVersionUID = -541451821784002680L;

	public List<KnowledgeEntity> dataList = null;
	
	public String state = "";
	public String countRow = "";
	public String currentPage = "";
	public String totalPages = "";
	
	public final static String STATE = "state";
	private final static String BODY = "body";
	
	public final static String COUNT_ROW = "countRow";
	public final static String CURRENT_PAGE = "currentPage";
	public final static String TOTAL_PAGES = "totalPages";
	public static KnowledgeListEntity parse(JSONObject jsonObject)throws JSONException{
		KnowledgeListEntity bean = new KnowledgeListEntity();
		if(jsonObject.has(STATE)){
			bean.state = jsonObject.getString(STATE);
		}
		/**
		 * 解析body节点
		 */
		if (jsonObject.has(BODY)) {
			
			JSONArray jsonArray = jsonObject.getJSONArray(BODY);
				
			bean.dataList = new ArrayList<KnowledgeEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				KnowledgeEntity entity = KnowledgeEntity.parse(object);
				bean.dataList.add(entity);
			}
			
		}
		if(jsonObject.has(COUNT_ROW)){
			bean.countRow = jsonObject.getString(COUNT_ROW);
		}
		if(jsonObject.has(CURRENT_PAGE)){
			bean.currentPage = jsonObject.getString(CURRENT_PAGE);
		}
		if(jsonObject.has(TOTAL_PAGES)){
			bean.totalPages = jsonObject.getString(TOTAL_PAGES);
		}
		/**
		 * -1表示:无数据
		 */
		if(bean.state.equals(HctConstants.RESULT_STATE_FAILD)
				&& bean.dataList == null){
			bean.dataList = new ArrayList<KnowledgeEntity>();
		}
		return bean;
	}
	/**
	 * 知识
	 * @author Tallen Zhai
	 *
	 */
	public static class KnowledgeEntity extends BaseEntity {

		private static final long serialVersionUID = 7228406311084104644L;
		public String rep_id = "";
		public String rep_title = "";
		public String rep_context = "";
		public String rep_comment = "";
		public String rep_type = "";
		public String rep_lcd = "";
		
		public final static String REP_ID = "id";
		public final static String REP_TITLE = "title";
		public final static String REP_CONTEXT = "context";
		public final static String REP_COMMENT = "comment";
		public final static String REP_TYPE = "repType";
		public final static String REP_LCD = "lcd";
		public static KnowledgeEntity parse(JSONObject jsonObject)
				throws JSONException {
			KnowledgeEntity bean = new KnowledgeEntity();
			if (jsonObject.has(REP_ID)) {
				bean.rep_id = jsonObject.getString(REP_ID);
			}
			if (jsonObject.has(REP_TITLE)) {
				bean.rep_title = jsonObject.getString(REP_TITLE);
			}
			if (jsonObject.has(REP_CONTEXT)) {
				bean.rep_context = jsonObject.getString(REP_CONTEXT);
			}
			if (jsonObject.has(REP_COMMENT)) {
				bean.rep_comment = jsonObject.getString(REP_COMMENT);
			}
			if (jsonObject.has(REP_TYPE)) {
				bean.rep_type = jsonObject.getString(REP_TYPE);
			}
			if (jsonObject.has(REP_LCD)) {
				bean.rep_lcd = jsonObject.getString(REP_LCD);
			}
			return bean;
		}
	}
}
